﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.Domain.Repositories;
using YAPACS.BusinessObjects.Managements.Exams;
using YAPACS.Organizations;
using YAPACS.Workflows;

namespace YAPACS.RisServer.Controllers;
[Authorize]
public class FilterController : AbpControllerBase
{
    private readonly ICurrentUserOrganization _currentUserOrganization;
    private readonly IRepository<ExamFilter, Guid> _filterRepository;
    private readonly IRepository<ApplicationUser, Guid> _userRepository;
    private readonly IRepository<ExamFlag, Guid> _examFlagRepository;

    public FilterController(ICurrentUserOrganization currentUserOrganization,
        IRepository<ExamFilter, Guid> filterRepository,
        IRepository<ApplicationUser, Guid> userRepository,
        IRepository<ExamFlag, Guid> examFlagRepository)
    {
        _currentUserOrganization = currentUserOrganization;
        _filterRepository = filterRepository;
        _userRepository = userRepository;
        _examFlagRepository = examFlagRepository;
    }

    [HttpGet("/api/filter/get-filter-by-type/{examFilterType}")]
    public async Task<IActionResult> GetFiltersAsync(ExamFilterType examFilterType)
    {
        var examFilters = new List<ExamFilter>();
        if (_currentUserOrganization != null
            && _currentUserOrganization.UserGroupId.HasValue
            && _currentUserOrganization.DepartmentId.HasValue)
        {
            var query = await _filterRepository.GetListAsync(e => e.IsActive && e.FilterType == examFilterType);
            //查询个人过滤
            var userFilters = query.Where(e => e.Department != null && e.Department.Oid == _currentUserOrganization.DepartmentId.Value
            && e.UserGroup != null && e.UserGroup.Oid == _currentUserOrganization.UserGroupId.Value
            && e.User != null && e.User.Oid == _currentUserOrganization.UserId);
            examFilters.AddIfNotContains(userFilters);
            //查询小组过滤
            var groupFilters = query.Where(e => e.Department != null && e.Department.Oid == _currentUserOrganization.DepartmentId.Value
            && e.UserGroup != null && e.UserGroup.Oid == _currentUserOrganization.UserGroupId.Value && e.User == null);
            examFilters.AddIfNotContains(groupFilters);
            //查询科室过滤
            var departmentFilters = query.Where(e => e.Department != null && e.Department.Oid == _currentUserOrganization.DepartmentId.Value && e.UserGroup == null);
            examFilters.AddIfNotContains(departmentFilters);
        }
        var user = await _userRepository.GetAsync(_currentUserOrganization.UserId);
        var defaultFilter = user.GetDefaultFilter(_currentUserOrganization.UserGroupId.Value, examFilterType);
        var filterItems = examFilters
            .OrderBy(f => f.IsDirectory ? 0 : 1)
            .ThenBy(f => f.Order)
            .Select(e => new
            {
                Id = e.Oid,
                ParentId = e.Parent?.Oid,
                Text = e.Name,
                e.IsDirectory,
                Selected = e == defaultFilter,
                Expanded = true
            }).ToList();
        if (examFilterType == ExamFilterType.Exam)
        {
            //收藏
            if (_currentUserOrganization != null
                && _currentUserOrganization.UserGroupId.HasValue
                && _currentUserOrganization.DepartmentId.HasValue)
            {
                var query = await _examFlagRepository.GetListAsync(e => e.IsActive);
                //查询科室过滤
                var departmentFlags = query.Where(e => e.Department != null && e.Department.Oid == _currentUserOrganization.DepartmentId.Value && e.UserGroup == null && e.IsActive).ToList();
                if (departmentFlags.Count != 0)
                {
                    Guid? parentId = null; Guid id = Guid.NewGuid();
                    filterItems.Add(new
                    {
                        Id = id,
                        ParentId = parentId,
                        Text = "科室收藏",
                        IsDirectory = true,
                        Selected = false,
                        Expanded = false
                    });
                    filterItems.AddRange(
                        departmentFlags.OrderBy(f => f.Order).ThenBy(f => f.CreationTime).Select(e => new
                        {
                            Id = e.Oid,
                            ParentId = e.Parent == null ? (Guid?)id : e.Parent.Oid,
                            Text = e.Name,
                            e.IsDirectory,
                            Selected = false,
                            Expanded = false
                        }));
                }
                //查询小组过滤
                var groupFlags = query.Where(e => e.Department != null && e.Department.Oid == _currentUserOrganization.DepartmentId.Value
                && e.UserGroup != null && e.UserGroup.Oid == _currentUserOrganization.UserGroupId.Value && e.User == null && e.IsActive).ToList();
                if (groupFlags.Count != 0)
                {
                    Guid? parentId = null; Guid id = Guid.NewGuid();
                    filterItems.Add(new
                    {
                        Id = id,
                        ParentId = parentId,
                        Text = "小组收藏",
                        IsDirectory = true,
                        Selected = false,
                        Expanded = false
                    });
                    filterItems.AddRange(
                        groupFlags.OrderBy(f => f.Order).ThenBy(f => f.CreationTime).Select(e => new
                        {
                            Id = e.Oid,
                            ParentId = e.Parent == null ? (Guid?)id : e.Parent.Oid,
                            Text = e.Name,
                            e.IsDirectory,
                            Selected = false,
                            Expanded = false
                        }));
                }
                //查询个人过滤
                var userFlags = query.Where(e => e.User != null && e.User.Oid == _currentUserOrganization.UserId && e.IsActive).ToList();
                if (userFlags.Count != 0)
                {
                    Guid? parentId = null; Guid id = Guid.NewGuid();
                    filterItems.Add(new
                    {
                        Id = id,
                        ParentId = parentId,
                        Text = "个人收藏",
                        IsDirectory = true,
                        Selected = false,
                        Expanded = false
                    });
                    filterItems.AddRange(
                        userFlags.OrderBy(f => f.Order).ThenBy(f => f.CreationTime).Select(e => new
                        {
                            Id = e.Oid,
                            ParentId = e.Parent == null ? (Guid?)id : e.Parent.Oid,
                            Text = e.Name,
                            e.IsDirectory,
                            Selected = false,
                            Expanded = false
                        }));
                }
            }
        }
        return Ok(new WorkflowOutput { Success = true, Data = filterItems });
    }
}
